/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Interfata;

import Arbori.ArboreFunctional;
import Parser.Parser;
import Parser.PostFixForm;
import Parser.TreeInsert;
import Visitor.CalculVisitor;
import Visitor.DerivareVisitor;
import java.util.ArrayList;

/**
 * Clasa CreazaCopac creaza arborele corespunzator expresiei date in interfata grafica,
 * expresie ce o primeste ca parametru in constructorul clasei.
 * @author AlinT
 *
 */
public class CreazaCopac extends Complet implements Command {

    ArboreFunctional arbore;
    double x;
    String calcul, derivat;
    Parser parser;

    public String getCalcul(){
        return calcul;
    }

    public String getDerivat(){
        return derivat;
    }

    /**
     * Constructor explicit ce parsa expresia si va crea arborele corespunzator
     * @param fct - functia ce o primeste ca parametru din interfata si a carui arbore trebuie creat
     */
     public CreazaCopac(String fct){
            this.parser=new PostFixForm();
            ArrayList<String> sirFix = parser.createPoz(fct);
            TreeInsert tree=new TreeInsert();
            this.arbore = tree.createTree(sirFix);
            this.x=0;
    }

 /**
  * Constructor explicit ce parsa expresia si va crea arborele corespunzator
  * @param fct - functia ce o primeste ca parametru din interfata si a carui arbore trebuie creat
  * @param x - valoarea in care trebuie calculata functia
  */
    public CreazaCopac(String fct, double x){
            Parser parser=new PostFixForm();
            ArrayList<String> sirFix = parser.createPoz(fct);
            TreeInsert tree=new TreeInsert();
            this.arbore = tree.createTree(sirFix);
            this.x=x;
    }

    public ArboreFunctional getArbore(){
        return arbore;
    }

    public void setArbore(ArboreFunctional a){
        this.arbore=a;
    }

    /**
     * Metoda execute() corespunzatoare sablonului Command - calculeaza derivata si valoarea functiei
     */
    public void execute() {
        CalculVisitor c=new CalculVisitor(this.x);
        DerivareVisitor d=new DerivareVisitor();
	arbore.acceptVisitor(c); calcul=""+c.getValue()+"";
	arbore.acceptVisitor(d); derivat=d.getDerivare();
        GUInterfata.setDerivata(derivat);
        GUInterfata.setValoare(calcul);
    }
}
